# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from taobao_category.items import ShopCategoryItem, ShopMainCategory
from taobao_category.models import Customer, ScrapyShopGoodsInfo, ShopMainCategoryTable
from taobao_category.sqlalchemy_engine import DBSession
from taobao_category.utils.utils import clear_ascii_extend_character


class TaobaoCategoryPipeline(object):
    def process_item(self, item, spider):
        return item

class TaobaoCategoryToDbPipeline(object):
    '''
    处理店铺分类
    '''
    def __init__(self):
        self.session = DBSession()

    def process_item(self, item, spider):
        if isinstance(item, ShopCategoryItem):
            info = ScrapyShopGoodsInfo()
            info.shop_id = item['shop_id']
            # info.title = item['title']
            # info.href = item['href']
            # info.price = item['price']
            # info.sales = item['sales']
            # info.evaluation_num = item['evaluation_num']
            info.cat_id = item['cat_id']
            # self.session.add(info)
            # self.session.commit()

        if isinstance(item, ShopMainCategory):
            info = ShopMainCategoryTable()
            info.shop_id = item['shop_id']
            info.main_category = item['main_category']
            # 中文中含有asci扩展字符集中的空白字符
            info.main_category = clear_ascii_extend_character(info.main_category)
            self.session.add(info)
            self.session.commit()